﻿using Microsoft.IdentityModel.Tokens;
using DataAcquisitionDAL;
using SqlSugar;

namespace DataAcquisitionBLL
{
    public class Template
    {
        //加密key
        static public string key = "datakeys";

        /// <summary>
        /// 试压模板列表
        /// </summary>
        /// <returns></returns>
        public List<TemplateModel> TemplateList()
        {
            List<TemplateModel> list = SqlSugarHelper.Inquireable<TemplateModel>(null);
            return list;
        }

        /// <summary> 添加试压模板
        /// 
        /// </summary>
        /// <param name="obj"></param>
        public Response AddTemplate(TemplateModel obj)
        {
            Response response = Response.DefaultResponse();
            if (obj.Number <= 0)
            {
                response.Msg = "请输入正确的模板编号";
                return response;
            }
            if (obj.PipePosition.IsNullOrEmpty())
            {
                response.Msg = "请输入管道位置";
                return response;
            }
            if (Convert.ToDouble(obj.DesignPressure) <= 0)
            {
                response.Msg = "请输入正确的设计压力";
                return response;
            }
            int stageCount = 0;

            if (Convert.ToDouble(obj.FirstStagePressure) > 0)
            {
                if (Convert.ToDouble(obj.FirstStageTime) <= 0)
                {
                    response.Msg = "请输入阶段一持续时间";
                    return response;
                }
                if (obj.FirstStageTimeUnit.IsNullOrEmpty())
                {
                    response.Msg = "请选择阶段一时间单位";
                    return response;
                }
                if (Convert.ToInt16(obj.FirstStageEstimateWay) <= 0)
                {
                    response.Msg = "请选择阶段一判断方式";
                    return response;
                }
                if (Convert.ToDouble(obj.FirstStageEstimateValue) <= 0)
                {
                    response.Msg = "请输入阶段一判断数值";
                    return response;
                }
                stageCount++;
            }

            if (Convert.ToDouble(obj.SecondStagePressure) > 0)
            {
                if (Convert.ToDouble(obj.SecondStageTime) <= 0)
                {
                    response.Msg = "请输入阶段二持续时间";
                    return response;
                }
                if (obj.SecondStageTimeUnit.IsNullOrEmpty())
                {
                    response.Msg = "请选择阶段二时间单位";
                    return response;
                }
                if (Convert.ToInt16(obj.SecondStageEstimateWay) <= 0)
                {
                    response.Msg = "请输入阶段二判断方式";
                    return response;
                }
                if (Convert.ToDouble(obj.SecondStageEstimateValue) <= 0)
                {
                    response.Msg = "请输入阶段二判断数值";
                    return response;
                }
                stageCount++;
            }

            if (Convert.ToDouble(obj.ThirdStagePressure) > 0)
            {
                if (Convert.ToDouble(obj.ThirdStageTime) <= 0)
                {
                    response.Msg = "请输入阶段三持续时间";
                    return response;
                }
                if (obj.ThirdStageTimeUnit.IsNullOrEmpty())
                {
                    response.Msg = "请选择阶段三时间单位";
                    return response;
                }
                if (Convert.ToInt16(obj.ThirdStageEstimateWay) <= 0)
                {
                    response.Msg = "请输入阶段三判断方式";
                    return response;
                }
                if (Convert.ToDouble(obj.ThirdStageEstimateValue) <= 0)
                {
                    response.Msg = "请输入阶段三判断数值";
                    return response;
                }
                stageCount++;
            }

            if (Convert.ToDouble(obj.IntensityStagePressure) > 0)
            {
                if (Convert.ToDouble(obj.IntensityStageTime) <= 0)
                {
                    response.Msg = "请输入强度段持续时间";
                    return response;
                }
                if (obj.IntensityStageTimeUnit.IsNullOrEmpty())
                {
                    response.Msg = "请选择强度段时间单位";
                    return response;
                }
                if (Convert.ToInt16(obj.IntensityStageEstimateWay) <= 0)
                {
                    response.Msg = "请输入强度段判断方式";
                    return response;
                }
                if (Convert.ToDouble(obj.IntensityStageEstimateValue) <= 0)
                {
                    response.Msg = "请输入强度段判断数值";
                    return response;
                }
                stageCount++;
            }

            if (Convert.ToDouble(obj.RigorStagePressure) > 0)
            {
                if (Convert.ToDouble(obj.RigorStageTime) <= 0)
                {
                    response.Msg = "请输入严密性持续时间";
                    return response;
                }
                if (obj.RigorStageTimeUnit.IsNullOrEmpty())
                {
                    response.Msg = "请选择严密性时间单位";
                    return response;
                }
                if (Convert.ToInt16(obj.RigorStageEstimateWay) <= 0)
                {
                    response.Msg = "请输入严密性判断方式";
                    return response;
                }
                if (Convert.ToDouble(obj.RigorStageEstimateValue) <= 0)
                {
                    response.Msg = "请输入严密性判断数值";
                    return response;
                }
                stageCount++;
            }
            
            if (stageCount == 0)
            {
                response.Msg = "当前模板无试压阶段，不能添加";
                return response;
            }
            obj.StageCount = stageCount;

            bool res = TemplateModel.RecordExistenceWithNumber(obj.Number);
            if (res)
            {
                response.Msg = string.Format("【{0}】模板已存在", obj.Number);
                return response;
            }
            List<TemplateModel> list = new List<TemplateModel>();
            list.Add(obj);
            int result = SqlSugarHelper.Insertable(list);
            if (result > 0)
            {
                response.Code = 200;
                response.Msg = string.Format("已添加【{0}】模板", obj.Number);
            }
            else
            {
                response.Msg = string.Format("模板添加失败");
            }
            return response;
        }

        /// <summary>
        /// 删除试压模板
        /// </summary>
        /// <param name="number"></param>
        /// <returns></returns>
        public Response DeleteTemplate(int number)
        {
            Response response = Response.DefaultResponse();
            Dictionary<string, Object> where = new Dictionary<string, Object>();
            where["number"] = number;
            List<TemplateModel> list = SqlSugarHelper.Inquireable<TemplateModel>(where);
            if (list.Count > 0)
            {
                int deleteRes = SqlSugarHelper.Deleteable(list);
                if (deleteRes > 0)
                {
                    response.Code = 200;
                    response.Msg = string.Format("试压模板已删除");
                }
                else
                {
                    response.Msg = string.Format("试压模板删除失败");
                }
                return response;
            }
            else
            {
                response.Msg = string.Format("【{0}】试压模板不存在，无法删除", number);
            }
            return response;
        }
    }
}
